// @ts-check
import axios from '@/utils/axios'
import { useCallback, useState } from 'react'

/**
 * 简单封装axios
 * @template T
 * @param {string} url 
 * @param {{[key:string]: any}} query 
 * @param { import("axios").AxiosRequestConfig } config 
 * @returns {import('../types').UseRequestReturn}
 */
export default function useRequest(url, query = {}, config = {}) {
  const [state, setState] = useState({
    loading: false,
    data: null
  })
  const trigger = useCallback(async (_query = {}, _config = {}) => {
    setState({
      loading: true,
      data: null
    })

    const res = await axios({
      method: 'GET',
      url,
      params: {...query, ..._query},
      ...config,
      ..._config
    })
    setState({
      loading: false,
      data: res.data
    })
    return state
  }, [])
  return [state, trigger]
}